﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Linq;
using System.Data.OleDb;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Dimok.Exceptions;
using Data.LINQ;

public partial class Controls_Upload_WaterFall : System.Web.UI.Page {
	protected void Page_Load(object sender, EventArgs e) {
		if (Request.Files.Count > 0) {
			byte[] file = new byte[Request.Files[0].InputStream.Length];
			Request.Files[0].InputStream.Read(file, 0, (int)Request.Files[0].InputStream.Length);
			var mb = new MotoBank();
			try {
				var waterFallID = Dimok.Common.ParseInt(Request["hdnWaterFallID"]);
				if (waterFallID == 0) throw new Exception("hdnWaterFallID field is not found in request.");
				var wfRec = mb.ts_PaymentWaterFalls.SingleOrDefault(r => r.ID == waterFallID);
				if (wfRec == null) throw new Exception("No WaterFall Info is found with ID:" + waterFallID);
				wfRec.Attachment = file;
				mb.SubmitChanges(ConflictMode.ContinueOnConflict);
				message.InnerHtml = "Done!";
			} catch (Exception exc) {
				ASPException.Publish(exc);
				message.InnerHtml = this.Server.UrlPathEncode(ASPException.GetMessages(exc));
			}
		}

	}
}
